<?php

/**
 * This is the model class for table "block".
 *
 * The followings are the available columns in table 'block':
 * @property integer $block_id
 * @property string $region
 * @property string $type
 * @property integer $order
 * @property string $title
 * @property string $params
 */
class Block extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Block the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'block';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('region, type, title', 'required'),
            array('order', 'numerical', 'integerOnly' => true),
            array('region, type', 'length', 'max' => 25),
            array('title', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('block_id, region, type, order, params', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'block_id' => 'Block',
            'region' => 'Region',
            'type' => 'Type',
            'order' => 'Order',
            'params' => 'Params',
        );
    }

    /**
     * Creates an active record instance.
     * This method is called by {@link populateRecord} and {@link populateRecords}.
     * You may override this method if the instance being created
     * depends the attributes that are to be populated to the record.
     * For example, by creating a record based on the value of a column,
     * you may implement the so-called single-table inheritance mapping.
     * @param array $attributes list of attribute values for the active records.
     * @return MyClass the active record
     */
    protected function instantiate($attributes) {
        if (!isset($attributes['type'])) { // When creating and $type_id has not defined yet
            return parent::instantiate($attributes);
        }

        switch ($attributes['type']){
            case HTMLBlock::TYPE :
                $className = 'HTMLBlock';
                break;
            default :
                $className = get_class($this);
                break;
        }

        $model = new $className(NULL);
        return $model;
    }

    protected function getEditUrl(){
        
    }
}